<?php
class RecipesController
{
    public $player;

    public $loaded = false;
    public $recipes = false;

    function  __construct($player) {
        $this->player = $player;
    }

    // out for client
    public function getAll()
    {
        $this->load();
        return $this->recipes;
    }

    // loading from database
    public function load()
    {
        if($this->loaded) return true;
        $query = 'SELECT `recipes` FROM `players_recipes` WHERE `user_id`='.$this->player->user_id;
        
        $data = _database::torow($query);
        if($data && is_array($data) && $data['recipes'])
        {
            $this->recipes = $this->unparse($data['recipes']);
        }
        else
        {
            $this->recipes = array();
        }
    }

    // unparse db string to array
    public function unparse($string)
    {
        $recipes = array();
        if($string)
        {
            $item_parts = explode('|',$string);
            if(is_array($item_parts))
            {
                foreach($item_parts as $part)
                {
                    list($id,$count) = explode(';',$part);
                    if((int)$count && (int)$id)
                    $recipes[(int)$id] = (int)$count;
                }
            }
        }
        return $recipes;
    }


    // parse array to db string
    public function parse()
    {

    }
}
?>
